<?php
require_once 'index1.php';
require_once 'class/admin.php';
?>
<link rel="stylesheet" href="/css/stylesheet.css"/>
<?php
if (isset($_SESSION['admin'])) {
    $admin = unserialize($_SESSION['admin']);
    echo "<div class='logStatuebar'><span class='welcome'> welcome admin</span>";
    echo "<div class=logout><a href='logout.php?attempt'>Log Out</a></div>";
    echo "<div class='userName'>" . $admin->getEmail() . "</div></div>";
    if ($admin->getInitial() != 3) {
        header("refresh:2; url=http://localhost:8080/adminInitial.php");
    }
} else {
    echo 'you need to login first';
    header("refresh:2; url=http://localhost:8080");
}
?>
<link rel="stylesheet" href="/css/jquery-ui.css"/>
<script src="/libs/jquery-1.9.1.js"></script>
<script src="/libs/jquery-ui.js"></script>

<script>
    $(function() {
        $("#tabs").tabs();
        $("#startDate").datepicker({ dateFormat: "yy-mm-dd" });
        $("#endDate").datepicker({ dateFormat: "yy-mm-dd" });
    });
    
    function addUser() {
        $.post("ajax1.php", $("#addUserForm").serialize(), function(data){$("#addUserFB").html(data)});
        setTimeout(function(){
            if($("#addUserFB").text()=="Create user successful"){
                var data="";
                $("#addUserFB").hide().html(data).fadeIn("slow");
                $("#addUserForm")[0].reset();
            }
                
        },2000);
    }
    
    function showUsers(){
        $.post("ajax1.php",{tip:"4"}, function(data){
            var user =data.split("<br>");
            user.pop();
            var finalString="<table cellspacing='2'><thead><tr><th>Give Name</th><th>Surname</th><th>Email</th><th>Initial Statue</th></tr></thead><tbody>";
            for (var i=0;i<user.length;i++){
                var userPart=user[i].split("|");
                var userString="";
                for(var j=0;j<userPart.length;j++){
                    //userString=userString+"<td>"+userPart[j]+"</td>";
                    if(j+1<userPart.length){
                     userString=userString+"<td>"+userPart[j]+"</td>";  
                    }else if(j+1==userPart.length){
                        if(userPart[j]=="0"){
                         userString=userString+"<td class='red'></td>";   
                        }else if(userPart[j]==1){
                         userString=userString+"<td class='green'></td>";
                         //userString=userString+"<td class='green'>"+userPart[j]+"</td>";
                        }
                        
                    }
                    
                }
                finalString =finalString+"<tr>"+userString+"<tr>";
            }
            finalString =finalString+"</tbody></table>";
            $("#showUserTable").html(finalString);
        });
    }
    
    function showRoster(){
        $.post("ajax1.php", {tip:"1"}, function(data){$("#showRoster").html(data)});
    }
    function createNewRoster(){
        $.post("ajax1.php",$("#createNewRosterForm").serialize() ,function(data){$("#createNewRosterFB").html(data)});
        setTimeout(function(){
            if($("#createNewRosterFB").text()=="create roster successful"){
                var data="";
                $("#createNewRosterFB").hide().html(data).fadeIn("slow");
                $("#createNewRosterForm")[0].reset();
            }
                
        },2000);
    }
    
    function generateRoster(){
        $.post("ajax1.php",{tip:"11"}, function(data){ 
            $("#showRoster").hide().html(data).fadeIn("slow");  
        });
        
    }
</script>

<div id="tabs">
    <ul>
        <li><a href="#tabs-1" onclick="showRoster()">Roster</a></li>
        <li><a href="#tabs-2">New Roster</a></li>
        <li><a href="#tabs-3">Add User</a></li>
        <li><a href="#tabs-4" onclick="showUsers()">Manage User</a></li>
    </ul>
    <div id="tabs-1">
        <p>show roster</p>
        <div id="showRoster"></div>
        <button id="generateRosterButton" onclick="generateRoster()">generate</button>

    </div>
    <div id="tabs-2">
        <div id="createNewRosterFB"></div>
        <div id="createNewRoster"><form id="createNewRosterForm">
                <input type="hidden" name="tip" value="2">
                <p>Start Date:<input type="text" name="startDate" id="startDate"/></p>
                <p>end Date:<input type="text" name="endDate" id="endDate"/></p>
            </form>
            <button id="button3" class="ui-state-default ui-corner-all" onclick="createNewRoster()">Choose</button>
        </div>
    </div>
    <div id="tabs-3">
        <p>New user</p>
        <div id="addUserFB"></div>
        <form id="addUserForm" class="addUser">
            <div class ="addUser1">GivenName:<input name="gName"/></div>
            <div class ="addUser2">SurName:<input name="sName"/></div>
            <div class ="addUser3">Email:<input name="email"/></div>
            <input name="tip" value="3" type="hidden"/>
        </form>
        <button id="addUser_button" onclick="addUser()">Add</button>
    </div>
    <div id="tabs-4">
        <p>show user</p>
        <div id="showUserTable">
        </div>


    </div>
</div>


<?php
require_once 'index2.php';
?>
